package application

import (
	"github.com/jinzhu/gorm"
	"time"
)

//CREATE TABLE `models` (
// `id` int(11) NOT NULL AUTO_INCREMENT,
// `name` varchar(255) DEFAULT NULL,
// `api_base` varchar(255) DEFAULT NULL,
// `api_key` varchar(255) DEFAULT NULL,
// `model` varchar(255) DEFAULT NULL,
// `created_at` datetime DEFAULT NULL,
// `updated_at` datetime DEFAULT NULL,
// PRIMARY KEY (`id`)
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

type Model struct {
	Id        int       `json:"id" gorm:"column:id;primary_key"`
	Name      string    `json:"name" gorm:"column:name"`
	ApiBase   string    `json:"api_base" gorm:"column:api_base"`
	ApiKey    string    `json:"api_key" gorm:"column:api_key"`
	Model     string    `json:"model" gorm:"column:model"`
	CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
	UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
}

func (m *Model) TableName() string {
	return "models"
}
func (m *Model) Create(db *gorm.DB) error {
	return db.Table(m.TableName()).Create(m).Error
}

func (m *Model) GetList(db *gorm.DB) (models []Model, err error) {
	err = db.Table(m.TableName()).Find(&models).Error
	return
}
func (m *Model) GetById(db *gorm.DB) error {
	return db.Table(m.TableName()).Where("id = ?", m.Id).First(m).Error
}
